package com.tgy.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.tgy.dao.DeptDao;
import com.tgy.entity.Dept;
import com.tgy.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/***
 * @ClassName: DeptController
 * @Description:
 * @Auther: 送一一块二向箔
 * @Date: 20:07 2022/1/10
 * @version : V1.0
 */
@RestController
@Slf4j
public class DeptController {

    @Resource
    DeptDao deptDao;
    /**
     * 查询部门信息
     * 如果根据id查询出现异常,则走hystrixGet这段备选代码
     * @return
     */
    @HystrixCommand(fallbackMethod = "hystrixGet")
    @RequestMapping("/list")//根据id查询
    public List<Dept> get(@RequestBody Dept dept){
        List<Dept> all = deptDao.getAll(dept);
        if (all.isEmpty()){
            throw new RuntimeException("这个id=>"+dept.getDeptNo()+",不存在该用户，或信息无法找到~");
        }
        return all;
    }
    /**
     * 根据id查询备选方案(熔断)
     * @return
     */
    public List<Dept> hystrixGet(@RequestBody Dept dept){
        List<Dept> lists = new ArrayList<>();

        Dept aa = new Dept().setDeptNo(0000L)
                .setDeptName("这个id=>" + dept.getDeptNo() + ",没有对应的信息,null---@Hystrix~")
                .setDbSource("在MySQL中没有这个数据库");
        lists.add(aa);
        return lists;
    }

}
